import {defineConfig, loadEnv} from 'vite';
import vue from '@vitejs/plugin-vue';
import path, {resolve} from 'path';
import Components from 'unplugin-vue-components/vite';
import AutoImport from 'unplugin-auto-import/vite';
import {createSvgIconsPlugin} from 'vite-plugin-svg-icons';
import {ElementPlusResolver} from 'unplugin-vue-components/resolvers';

/** 配置项文档：https://cn.vitejs.dev/config */
export default defineConfig(({mode}) => {
    const env = loadEnv(mode, './env');
    console.log('环境', env);
    return {
        base: env.VITE_PUBLIC_PATH,
        publicDir: 'tess',
        plugins: [
            vue(),
            AutoImport({
                resolvers: [ElementPlusResolver()]
            }),
            Components({
                resolvers: [ElementPlusResolver()]
            }),
            /** SVG */
            createSvgIconsPlugin({
                iconDirs: [path.resolve(process.cwd(), 'src/icons/svg')],
                symbolId: 'icon-[dir]-[name]'
            })
        ],
        server: {
            https: false, // 是否开启 HTTPS
            host: true, // host: "0.0.0.0" 设置 host: true 才可以使用 Network 的形式，以 IP 访问项目
            cors: true, // 跨域设置允许
            strictPort: false, // 端口被占用时，是否直接退出
            open: true, // 是否自动打开浏览器
            proxy: {
                '/web': {
                    target: env.VITE_BASE_URL, // 后端接口
                    changeOrigin: true, // 是否允许跨域
                    headers: {
                        // Cookie: 'test'
                    },
                    rewrite: (path) => path.replace(/^\/api/, '')
                }
            }
        },
        resolve: {
            alias: {
                // @ 替代为 src
                '@': resolve(__dirname, 'src'),
                // @component 替代为 src/component
                '@components': resolve(__dirname, 'src/components')
            }
        }
    };
});
